(function() {
    angular.module('app').factory('tableBrandShopsService', tableBrandShopsService);

    tableBrandShopsService.$inject = ['util', '$q', 'atomicNotifyService','$rootScope'];

    function tableBrandShopsService(util, $q, atomicNotifyService,$rootScope) {

        var monitorShopCurIndex = -1; //店铺详情的序号


        var monitorDataCurIndex = -1; //日监控所在的序号

        return {
            get: _get,

            shopControllerGet: _shopControllerGet,

            monitorControllerGet: _monitorControllerGet,

            deleteCollection: _deleteCollection,

            getShopBasic: _getShopBasic,
            shopBasicInit: _shopBasicInit,

            getMonitorData: _getMonitorData,
            monitorDataInit: _monitorDataInit
        }

        function _get(json) {
            var defer = $q.defer();

            util.$get({
                url: '/marketconditions/shop/getShopsByBrand.htm',
                data: json
            }).then(function(resp) {

                defer.resolve(resp.value);

            }, function(err) {

                defer.reject(err);

            });

            return defer.promise;
        }

        function _shopControllerGet(json) {

            var defer = $q.defer();

            util.$get({
                url: '/marketconditions/shop/getShops.htm',
                data: json
            }).then(function(resp) {

                defer.resolve(resp.value);

            }, function(err) {

                defer.reject(err);

            });

            return defer.promise;
        }


        function _monitorControllerGet() {

            var defer = $q.defer();

            util.$get({
                url: '/marketconditions/collection/getCollection.htm',
                data: {
                    type: 'shop'
                }
            }).then(function(resp) {

                defer.resolve(resp.value);

            }, function(err) {

                defer.reject(err);

            });

            return defer.promise;
        }

        function _deleteCollection(shop) {

            var defer = $q.defer();

            util.$get({
                url: '/marketconditions/collection/deleteCollections.htm',
                data: {
                    targetId: shop.userId,
                    type: 'shop'
                }
            }).then(function(resp) {

                shop.collectionStatus = null;
                var message = '取消监控' + shop.shopName;
                atomicNotifyService.info(message);

                defer.resolve();

            }, function(err) {

                var message = '取消监控' + shop.shopName + '失败!';
                atomicNotifyService.warning(message);

                defer.reject();
            });

            return defer.promise;
        }

        function _getShopBasic(event, index) {
            var vm = this;

            //手机端
            if ($(window).width() < 992) {
                $rootScope.$emit('Mobile_Component_EV', { uid: vm.table.shops[index].userId });
                return;
            }
            // 清除日监控详情
            if (monitorDataCurIndex != -1) {
                console.log("monitorDataCurIndex:" + monitorDataCurIndex);
                vm.table.shops.splice(monitorDataCurIndex + 1, 1);
                index = index > monitorDataCurIndex ? index - 1 : index;
                monitorDataCurIndex = -1;
            }


            //monitorDataCurIndex 已存在
            if (monitorShopCurIndex == index) {
                vm.table.shops.splice(index + 1, 1);
                monitorShopCurIndex = -1;
                return;
            }

            // 清空之前的数据
            if (monitorShopCurIndex >= 0) {
                vm.table.shops.splice(monitorShopCurIndex + 1, 1);
            }

            if (monitorShopCurIndex >= 0 && index > monitorShopCurIndex) {
                index--;
            }

            vm.table.shops.splice(index + 1, 0, {
                monitorDetail: true,
                uid: vm.table.shops[index].userId,
            });
            monitorShopCurIndex = index;
        }

        function _shopBasicInit() {
            monitorShopCurIndex = -1; //店铺详情的序号
        }

        function _getMonitorData(event, index) {
            var vm = this;

            //手机端
            if ($(window).width() < 992) {
                $rootScope.$emit('Mobile_Component_EV', { targetId: vm.table.shops[index].userId, type: 'shop' });
                return;
            }

            // 清除店铺详情
            if (monitorShopCurIndex != -1) {
                console.log("monitorShopCurIndex:" + monitorShopCurIndex);
                vm.table.shops.splice(monitorShopCurIndex + 1, 1);
                index = index > monitorShopCurIndex ? index - 1 : index;
                monitorShopCurIndex = -1;
            }


            //monitorDataCurIndex 已存在
            if (monitorDataCurIndex == index) {
                vm.table.shops.splice(index + 1, 1);
                monitorDataCurIndex = -1;
                return;
            }

            // 清空之前的数据
            if (monitorDataCurIndex >= 0) {
                vm.table.shops.splice(monitorDataCurIndex + 1, 1);
            }

            if (monitorDataCurIndex >= 0 && index > monitorDataCurIndex) {
                index--;
            }

            vm.table.shops.splice(index + 1, 0, {
                monitorDetail: true,
                targetId: vm.table.shops[index].userId,
            });
            monitorDataCurIndex = index;
        }

        function _monitorDataInit() {
            monitorDataCurIndex = -1; //日监控所在的序号
        }
    }
})();
